import Model from '../abstract';
import downloadFiles, { downloadFile } from '../../utils/downImgUtils';

export default class ModelCatalogImage extends Model {

      constructor() {
            super();
            this.mainTable = 'product_image';
            this.pk = 'image_id';
            this.serverPk = 'server_image_id';
            this.versionName = 'version';
            this.dateAddedName = 'date_added';
            this.lastUpdatedName = 'last_updated';
      }

      /**
       * 获取商品的图片
       */
      async getGallery(productId) {
            let main = null;
            let more = [];
            let images = await this.fetchRows("SELECT * FROM `product_image` WHERE `product_id` = ? ORDER BY `sort_order` ASC", [productId]);
            let i = 0;
            for (image of images) {
                  if (i == 0) {
                        main = image;
                  } else {
                        more.push(image);
                  }
                  i++;
            }
            return {
                  main: main,
                  more: more
            };
      }

      /**
       * 获取商品的所有images
       */
      async getImages(productId) {
            return await this.fetchRows("SELECT * FROM `product_image` WHERE `product_id` = ? ORDER BY `sort_order` ASC", [productId]);
      }

      /**
       * 保存商品图片
       */
      async saveImages(productId, images = []) {
            if (productId > 0) {
                  if (!Array.isArray(images)) {
                        images = [];
                  }
                  for (image of images) {
                        await this.saveImage(productId, image);
                  }
            }
      }

      /**
       * 保存商品图片
       */
      async saveImage(productId, image) {
            let imageId = 0;
            if (productId > 0 && typeof(image) == 'object') {
                  image.product_id = productId;
                  imageId = parseInt(image.image_id);
                  imageId = await this.save(image, imageId);
            }
            return imageId;
      }

      /**
       * 保存商品options
       */
      async saveImagesByServerPk(productId, images = []) {
            if (productId > 0) {
                  if (!Array.isArray(images)) {
                        images = [];
                  }
                  for (image of images) {
                        await this.saveImageByServerPk(productId, image);
                  }
            }
      }

      /**
       * 保存商品option
       */
      async saveImageByServerPk(productId, image) {
            let imageId = 0;
            if (productId > 0 && typeof(image) == 'object') {
                  let serverImageId = parseInt(image.server_image_id);
                  let url = null;
                  if (image.url) {
                        url = image.url;
                        delete image.url;
                  }
                  if (serverImageId > 0 && url !== null) {
                        let version = await this.getVersionByServerPk(serverImageId);
                        if (version < image.version) {
                              let name = sprintf("product/%d/%s", productId, image.name);
                              let file = await downloadFile(url, name, true);
                              if (file !== null) {
                                    console.log("product image file >>>> ", file);
                                    image.product_id = productId;
                                    image.name = file;
                                    imageId = await this.saveByServerPk(image, serverImageId);
                              }
                        }
                  }
            }
            return imageId;
      }

      /**
       * 删除商品分类
       */
      async removeImage(imageId) {
            let image = await this.get(imageId);
            if (image) {
                  // 删除图片
                  if (image.name) {

                  }
                  return await this.remove(imageId);
            }
            return false;
      }

      /**
       * 删除商品分类
       */
      async removeImageByServerPk(serverPk) {
            let imageId = await this.getIdByServerPk(serverPk);
            if (imageId > 0) {
                  return await this.removeImage(imageId);
            }
            return false;
      }

      /**
       * 按照商品id删除图片
       */
      async removeImages(productId) {
            let images = await this.getImages(productId);
            for (image of images) {
                  let imageId = image.image_id;
                  await this.removeImage(imageId);
            }
            return true;
      }

}